﻿using MediatR;
using T.ApprovalRecord;
using T.Materials.API.Application.Command;
using T.Materials.Domain.Model;
using T.Materials.Repository.MaterialsInterface;
using static T.Materials.Domain.Enum.Enums;

namespace T.Materials.API.Application.CommandHandler
{
    public class MapprwsHnalder : IRequestHandler<MApprow, int>
    {
        /// <summary>
        /// 
        /// </summary>
        protected readonly IMApprow mApprow;

        /// <summary>
        /// 
        /// </summary>
        protected readonly IMaterialStoreRepository Material;

        protected readonly IStorageMaterialsRepository storageMaterialsRepository;

        public MapprwsHnalder(IMApprow mApprow, IMaterialStoreRepository material, IStorageMaterialsRepository storageMaterialsRepository)
        {
            this.mApprow = mApprow;
            Material = material;
            this.storageMaterialsRepository = storageMaterialsRepository;
        }

        public async Task<int> Handle(MApprow request, CancellationToken cancellationToken)
        {
            var sss = await Material.GetModelAsync((int)request.MaterialStoreId);
            if (request.ApprovalResult == "同意")
            {
                ////审批详情
                //var approvalRecord = new MApproval()
                //{
                //    CompanyID = request.CompanyID,
                //    User_Id = request.User_Id,
                //    ApprovalResult = request.ApprovalResult,
                //    ApprovalNotes = request.ApprovalNotes,
                //    Approver = request.Approver,
                //    MaterialRequisitionId = request.MaterialRequisitionId,
                //};
                await mApprow.AddAsync(request);
                sss.ApprovalStatus = ApprovalStatus.已通过;
                sss.Approver = request.Approver;
                await Material.UpdateAsync(sss);
            }
            if (request.ApprovalResult == "同意并转批")
            {
                //审批详情
                //var approvalRecord = new MApproval()
                //{
                //    CompanyID = request.CompanyID,
                //    User_Id = request.User_Id,
                //    ApprovalResult = request.ApprovalResult,
                //    ApprovalNotes = request.ApprovalNotes,
                //    Approver = request.Approver,
                //    MaterialRequisitionId = request.MaterialRequisitionId,
                //    Subgrantor = request.Subgrantor
                //};
                await mApprow.AddAsync(request);
            }

            if (request.ApprovalResult == "拒绝")
            {
                //审批详情
                var approvalRecord = new MApproval()
                {
                    CompanyID = request.CompanyID,
                    User_Id = request.User_Id,
                    ApprovalResult = request.ApprovalResult,
                    ApprovalNotes = request.ApprovalNotes,
                    Approver = request.Approver,
                    MaterialRequisitionId = request.MaterialRequisitionId,
                };
                await mApprow.AddAsync(approvalRecord);
                sss.ApprovalStatus = ApprovalStatus.已拒绝;
                sss.Approver = request.Approver;
                await Material.UpdateAsync(sss);
            }
            return 1;
        }
    }
}
